package com.auction.controller.user;

import com.auction.dto.OrderDTO;
import com.auction.pojo.Result;
import com.auction.service.OrderService;
import com.auction.service.SellerService;
import com.auction.utils.BaseContext;
import com.auction.vo.OrderVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController("userOrderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private SellerService sellerService;

    /**
     * 创建订单
     * 
     * @param orderDTO 订单信息
     * @return 订单ID
     */
    @PostMapping
    public Result<Long> create(@RequestBody OrderDTO orderDTO) {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}创建订单：{}", userId, orderDTO);
        Long orderId = orderService.create(orderDTO, userId);
        return Result.success(orderId);
    }

    /**
     * 获取订单详情
     * 
     * @param id 订单ID
     * @return 订单详情
     */
    @GetMapping("/{id}")
    public Result<OrderVO> getById(@PathVariable Long id) {
        log.info("查询订单详情：id={}", id);
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     * 
     * @param id 订单ID
     * @return 取消结果
     */
    @PutMapping("/{id}/cancel")
    public Result<Boolean> cancel(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}取消订单：id={}", userId, id);
        boolean result = orderService.cancel(id, userId);
        return Result.success(result);
    }

    /**
     * 支付订单
     *
     * @param id 订单ID
     * @return 支付结果
     */
    @PutMapping("/{id}/pay")
    public Result<Boolean> pay(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}支付订单：id={}", userId, id);
        boolean result = orderService.pay(id, userId);
        return Result.success(result);
    }

    /**
     * 确认收货
     * 
     * @param id 订单ID
     * @return 确认结果
     */
    @PutMapping("/{id}/confirm")
    public Result<Boolean> confirm(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        log.info("用户{}确认收货：id={}", userId, id);
        boolean result = orderService.confirm(id, userId);
        return Result.success(result);
    }

    /**
     * 发货
     *
     * @param id             订单ID
     * @param trackingNumber 物流单号
     * @return 发货结果
     */
    @PutMapping("/{id}/ship")
    public Result<Boolean> ship(@PathVariable Long id, @RequestParam String trackingNumber) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("卖家{}发货：订单id={}, 物流单号={}", sellerId, id, trackingNumber);
        boolean result = orderService.ship(id, sellerId, trackingNumber);
        return Result.success(result);
    }


    /**
     * 分页查询买家订单列表
     * 
     * @param pageNum     页码
     * @param pageSize    每页大小
     * @param orderStatus 订单状态
     * @return 订单分页列表
     */
    @GetMapping("/buyer")
    public Result<PageInfo<OrderVO>> buyer(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer orderStatus) {
        Long userId = BaseContext.getCurrentId();
        log.info("查询买家订单：userId={}, 状态={}, 页码={}, 每页记录数={}",
                userId, orderStatus, pageNum, pageSize);
        PageInfo<OrderVO> pageInfo = orderService.pageBuyer(pageNum, pageSize, userId, orderStatus);
        return Result.success(pageInfo);
    }

    /**
     * 分页查询卖家订单列表
     *
     * @param pageNum     页码
     * @param pageSize    每页大小
     * @param orderStatus 订单状态
     * @return 订单分页列表
     */
    @GetMapping("/seller")
    public Result<PageInfo<OrderVO>> seller(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Integer orderStatus) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("查询卖家订单：sellerId={}, 状态={}, 页码={}, 每页记录数={}",
                sellerId, orderStatus, pageNum, pageSize);
        PageInfo<OrderVO> pageInfo = orderService.pageSeller(pageNum, pageSize, sellerId, orderStatus);
        return Result.success(pageInfo);
    }

}